﻿//
// Copyright (c) 2010
// by Ashish Agrawal and Chris Paterra
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Web.Mvp;
using SimpleBlog.Components.Views;
using SimpleBlog.Components.Models;
using WebFormsMvp;
using SimpleBlog.Components.Common;
using SimpleBlog.Components.Entities;
using SimpleBlog.Components.Presenters;
using DotNetNuke.Services.Localization;

namespace SimpleBlog 
{
	[PresenterBinding(typeof(EditEntryPresenter))]
	public partial class EditEntry : ModuleView<EditEntryModel>, IEditEntryView {

		public event EventHandler Cancel;
		public event EventHandler Delete;
		public event EventHandler<UIEventArgs<EntryInfo>> Save;

		#region Event Handlers

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void CmdCancelClick(object sender, EventArgs e) 
		{
			Cancel(sender, e);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void CmdDeleteClick(object sender, EventArgs e)
		{
			Delete(sender, e);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void CmdSaveClick(object sender, EventArgs e)
		{

			if (valSummaryReq.Enabled)
			{
				if (teSummary.Text.Trim().Length < 1) {
					valSummaryReq.IsValid = false;
					return;
				}
			}

			if (teSummary.Text.Length > 1024)
			{
				valSummary.IsValid = false;
				return;
			}

			if (teContent.Text.Trim().Length < 1)
			{
				valContent.IsValid = false;
				return;
			}

			Model.BlogEntry = new EntryInfo
			                  	{
			                  		Title = txtTitle.Text.Trim(),
			                  		Summary = teSummary.Text.Trim(),
								BlogContent = teContent.Text.Trim(),
			                  		AllowComments = chkAllowComments.Checked,
			                  		Publish = chkPublish.Checked,
								ContentItemId = Convert.ToInt32(txtContentItemId.Text)
			                  	};

			// Add categories
			Model.BlogEntry.Terms.Clear();
			Model.BlogEntry.Terms.AddRange(tsTerms.Terms);

			Save(this, new UIEventArgs<EntryInfo>(Model.BlogEntry));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="show"></param>
		public void ShowAuditControl(bool show) {
			ctlAudit.Visible = show;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="show"></param>
		public void ShowDeleteButton(bool show) {
			cmdDelete.Visible = show;
		}

		/// <summary>
		/// 
		/// </summary>
		public void Refresh() {
			ctlAudit.CreatedDate = Model.BlogEntry.CreatedOnDate.ToString();
			ctlAudit.CreatedByUser = Model.BlogEntry.CreatedByUser;
			ctlAudit.LastModifiedDate = Model.BlogEntry.LastModifiedOnDate.ToString();
			ctlAudit.LastModifiedByUser = Model.BlogEntry.LastModifiedByUser;

			if (Page.IsPostBack) return;
			txtTitle.Text = Model.BlogEntry.Title;
			teSummary.Text = Server.HtmlDecode(Model.BlogEntry.Summary);
			teContent.Text = Server.HtmlDecode(Model.BlogEntry.BlogContent);
			chkAllowComments.Checked = Model.BlogEntry.AllowComments;
			chkPublish.Checked = Model.BlogEntry.Publish;
			txtContentItemId.Text = Model.BlogEntry.ContentItemId.ToString();

			if (Model.BlogEntry.PublishOnDate != DateTime.MinValue)
			{
				txtPublishOnDate.Text = Model.BlogEntry.PublishOnDate.ToShortDateString();
			}

			tsTerms.PortalId = ModuleContext.PortalId;
			tsTerms.Terms = Model.BlogEntry.Terms;
			tsTerms.DataBind();

			// change below to integrate module setting (when ready)
			valSummaryReq.Enabled = false;
			cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
		}

		#endregion

	}
}